﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace WEALTH.Manager.Customer
{
    public partial class InformationListGift : BasePage
    {
        private ExcuteDao InformationManage = new ExcuteDao();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SetMenu();
            }
            if (Request.QueryString["UrlType"] != null && Request.QueryString["UrlType"].Equals("menu"))
            {
                Session["SortExpression_InformationList"] = null;
                Session["SortDirection_InformationList"] = null;
                Session["txtListTitle"] = null;
                Session["ddlListMenu"] = null;
                Session["txtListFrom"] = null;
                Session["txtListjianjie"] = null;
            }

            if (Session["UrlType"] != null)
            {
                if (Session["UrlType"].Equals("EditInformation"))
                {
                    setSessionData();
                    if (Session["InformationListIndex"] != null && string.IsNullOrEmpty(Request.QueryString["pageIndex"]))
                    {
                        gvInformation.PageIndex = (int)Session["InformationListIndex"];
                    }

                    Session["UrlType"] = null;
                }
            }
            if (!string.IsNullOrEmpty(Request.QueryString["pageIndex"]))
            {
                string pageIndex = Request.QueryString["pageIndex"];
                int index = Convert.ToInt32(pageIndex);
                gvInformation.PageIndex = index;

                Session["InformationListIndex"] = index;

                setSessionData();
            }
            //设置分页状态
            if (Session["SortExpression_InformationList"] != null)
            {
                this.gvInformation.Attributes["SortExpression_InformationList"] = Session["SortExpression_InformationList"].ToString();
                this.gvInformation.Attributes["SortDirection_InformationList"] = Session["SortDirection_InformationList"].ToString();
                setSortColumnStyle(this.gvInformation, Session["SortExpression_InformationList"].ToString(), Session["SortDirection_InformationList"].ToString());
            }
            //setSortColumnStyle(gvInformation, this.gvInformation.Attributes["SortExpression_InformationList"].ToString(),
            //     this.gvInformation.Attributes["SortDirection_InformationList"].ToString());
            if (!IsPostBack)
            {
                ViewState["InformationAll"] = InformationManage.Get_ListALL(
                                                SpecialCharHandler.HtmlEncode(txtListTitle.Text),
                                                SpecialCharHandler.HtmlEncode(txtListjianjie.Text),                    
                                                -1,
                                                SpecialCharHandler.HtmlEncode(txtListFrom.Text));

                //初次加载清空分页状态
                //if (string.IsNullOrEmpty(Request.QueryString["pageIndex"]) && Session["UrlType"] == null)
                //{
                //    Session["SortExpression_InformationList"] = null;
                //    Session["SortDirection_InformationList"] = null;
                //}
                SearchInformationBindData();
            }
        }
        private void SetMenu()
        {
            ddlListMenu.Items.Clear();
            ddlListMenu.Items.Add(new ListItem("", "-1"));

            DataTable menulistdt = InformationManage.Get_LeftMenu();
            for (int i = 0; i < menulistdt.Rows.Count; i++)
            {
                ddlListMenu.Items.Add(new ListItem(menulistdt.Rows[i]["MenuName"].ToString(), menulistdt.Rows[i]["MenuID"].ToString()));
            }
        }
        private void setSessionData()
        {
            //检索条件记录
            if (Session["txtListTitle"] != null)
            {
                txtListTitle.Text = SpecialCharHandler.HtmlDiscode(Session["txtListTitle"].ToString());
            }
            if (Session["ddlListMenu"] != null)
            {
                ddlListMenu.SelectedValue = Session["ddlListMenu"].ToString();
            }
            if (Session["txtListFrom"] != null)
            {
                txtListFrom.Text = SpecialCharHandler.HtmlDiscode(Session["txtListFrom"].ToString());

            }
            if (Session["txtListjianjie"] != null)
            {
                txtListjianjie.Text = SpecialCharHandler.HtmlDiscode(Session["txtListjianjie"].ToString());

            }

            if (Session["InformationListIndex"] != null)
            {
                gvInformation.PageIndex = (int)Session["InformationListIndex"];
            }
        }
        #region 信息列表事件

        /// <summary>
        /// 信息列表数据加载时触发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvInformation_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("id", e.Row.RowIndex.ToString());
                e.Row.Attributes.Add("onKeyDown", "SelectRow();");
                e.Row.Attributes.Add("onClick", "MarkRow(" + e.Row.RowIndex.ToString() + ");");
                ((HyperLink)e.Row.Cells[0].Controls[0]).NavigateUrl = "../Customer/EditInformation.aspx?isGift=1&InformationID="
                    + SpecialCharHandler.HtmlDiscode(((HyperLink)e.Row.Cells[0].Controls[0]).Text).Replace("&", "amp;");

                e.Row.Cells[1].Text = SpecialCharHandler.HtmlDiscode(e.Row.Cells[1].Text);

                string jianjie = SpecialCharHandler.HtmlDiscode(e.Row.Cells[2].Text);
                e.Row.Cells[2].ToolTip = jianjie;
                if (jianjie.Length > 15)
                {
                    jianjie = jianjie.Substring(0, 15) + "...";
                }
                e.Row.Cells[2].Text = jianjie;
                e.Row.Cells[3].Text = SpecialCharHandler.HtmlDiscode(e.Row.Cells[3].Text);
                e.Row.Cells[4].Text = SpecialCharHandler.HtmlDiscode(e.Row.Cells[4].Text);

            }
        }
        #endregion

        /// <summary>
        /// 搜索按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            ViewState.Clear();
            ViewState["InformationAll"] = InformationManage.Get_ListALL(
                                                SpecialCharHandler.HtmlEncode(txtListTitle.Text),
                                                SpecialCharHandler.HtmlEncode(txtListjianjie.Text),                    
                                                Convert.ToInt32(ddlListMenu.SelectedValue),
                                                SpecialCharHandler.HtmlEncode(txtListFrom.Text));
            //清空分页状态
            Session["SortExpression_InformationList"] = null;
            Session["SortDirection_InformationList"] = null;

            //检索条件记录
            Session["txtListTitle"] = SpecialCharHandler.HtmlEncode(txtListTitle.Text);
            Session["txtListjianjie"] = SpecialCharHandler.HtmlEncode(txtListjianjie.Text);
            Session["ddlListMenu"] = Convert.ToInt32(ddlListMenu.SelectedValue);
            Session["txtListFrom"] = SpecialCharHandler.HtmlEncode(txtListFrom.Text);

            SearchInformationBindData();

        }

        /// <summary>
        /// 选择页面功能
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvInformation_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.gvInformation.PageIndex = e.NewPageIndex;
            pageIndex = e.NewPageIndex;
            Session["InformationListIndex"] = pageIndex;
            SearchInformationBindData();
        }

        /// <summary>
        /// GridView排序事件
        /// </summary>
        protected void gvInformation_Sorting(object sender, GridViewSortEventArgs e)
        {
            // 从事件参数获取排序数据列
            string SortExpression_InformationList = e.SortExpression.ToString();
            //// 假定为排序方向为“顺序”
            string SortDirection_InformationList = "ASC";
            // “ASC”与事件参数获取到的排序方向进行比较，进行GridView排序方向参数的修改
            if (SortExpression_InformationList == this.gvInformation.Attributes["SortExpression_InformationList"])
            {
                //获得下一次的排序状态
                SortDirection_InformationList = (this.gvInformation.Attributes["SortDirection_InformationList"].ToString() == SortDirection_InformationList ? "DESC" : "ASC");
            }
            // 重新设定GridView排序数据列及排序方向
            this.gvInformation.Attributes["SortExpression_InformationList"] = SortExpression_InformationList;
            this.gvInformation.Attributes["SortDirection_InformationList"] = SortDirection_InformationList;
            //记录分页状态
            Session["SortExpression_InformationList"] = SortExpression_InformationList;
            Session["SortDirection_InformationList"] = SortDirection_InformationList;

            setSortColumnStyle(this.gvInformation, SortExpression_InformationList, SortDirection_InformationList);
            this.SearchInformationBindData();
        }

        /// <summary>
        /// 绑定到GridView
        /// </summary>
        private void SearchInformationBindData()
        {
            // 获取GridView排序数据列及排序方向
            string SortExpression_InformationList = this.gvInformation.Attributes["SortExpression_InformationList"];
            string SortDirection_InformationList = this.gvInformation.Attributes["SortDirection_InformationList"];
            // 调用业务数据获取方法
            DataTable dtBind = (DataTable)ViewState["InformationAll"];

            //页面数据总数
            recordCount = dtBind.Rows.Count;

            // 根据GridView排序数据列及排序方向设置显示的默认数据视图
            if ((!string.IsNullOrEmpty(SortExpression_InformationList)) && (!string.IsNullOrEmpty(SortDirection_InformationList)))
            {
                dtBind.DefaultView.Sort = string.Format("{0} {1}", SortExpression_InformationList, SortDirection_InformationList);
            }
            // GridView绑定并显示数据
            this.gvInformation.DataSource = dtBind;
            this.gvInformation.DataBind();

            pageCount = gvInformation.PageCount;
            pageIndex = gvInformation.PageIndex;

        }
        protected void gvInformation_DataBound(object sender, EventArgs e)
        {
            if (Session["Edit_InformationID"] != null)
            {
                foreach (GridViewRow gvr in gvInformation.Rows)
                {
                    if (((HyperLink)gvr.Cells[0].Controls[0]).Text.Equals(
                        SpecialCharHandler.HtmlDiscode(Session["Edit_InformationID"].ToString())))
                    {
                        //gvInformation.SelectedIndex = gvr.RowIndex;
                        //gvInformation.Rows[gvr.RowIndex].BackColor = System.Drawing.Color.FromName("#D8EBF0");
                        ClientScript.RegisterStartupScript(GetType(), "SelectRow", "<script>MarkRow(" + gvr.RowIndex + "); </script>");

                    }
                }
                Session["Edit_InformationID"] = null;
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {

            int isok = 0;

            bool ischecked = false;
            for (int i = 0; i <= gvInformation.Rows.Count - 1; i++)
            {
                CheckBox CheckBox = (CheckBox)gvInformation.Rows[i].FindControl("chkdelete");
                if (CheckBox.Checked == true)
                {
                    string Informationid = ((HyperLink)gvInformation.Rows[i].Cells[0].Controls[0]).Text;
                    isok = InformationManage.Delete_Information(Convert.ToInt32(Informationid));
                    ischecked = true;
                }

            }
            if (ischecked)
            {
                ischecked = false;
                if (isok == -1)
                {
                    ShowMsg("删除信息成功！", 0);
                    ViewState["InformationAll"] = InformationManage.Get_ListALL(SpecialCharHandler.HtmlEncode(txtListTitle.Text),
                                                SpecialCharHandler.HtmlEncode(txtListjianjie.Text),                    
                                                Convert.ToInt32(ddlListMenu.SelectedValue),
                                                SpecialCharHandler.HtmlEncode(txtListFrom.Text));
                    SearchInformationBindData();
                }
                else
                {
                    ShowMsg("删除信息失败！", 1);
                }
            }
        }
    }
}